# coding:utf-8

import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpserver
import json

from tornado.options import options
from tornado.web import RequestHandler

tornado.options.define("port", default=8000, type=int, help="run server on the given port")


class IndexHandler(RequestHandler):
    """请求处理类"""
    def get(self):
        self.write("hello")


class UploadHandler(RequestHandler):
    """"""
    def post(self):
        # print self.request.files
        file_body = self.request.files.get("pic")[0]["body"]
        # file = open("./pic.jpg", "wb")
        # file.write(file_body)
        # file.close()
        with open("./pic.jpg", "wb") as file:
            file.write(file_body)
        self.write("OK")

if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        [
            (r"/", IndexHandler),
            (r"/upload", UploadHandler)
        ],
        debug=True
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()